<?php

namespace app\teacher\controller;

use app\admin\model\classes\Teacher;
use app\admin\model\User;
use app\common\model\TeacherQuestion as TeacherQuestionModel;
use app\common\model\UserBadge;
use think\Db;

class TeacherQuestion extends Base
{
    protected $model = null;
    public function __construct(TeacherQuestionModel $model)
    {
        parent::__construct();
        $this->model = $model;
    }

    public function lists()
    {
        $page = $this->request->param('page', 1);
        $limit = $this->request->param('limit', 10);
        $teacher = Teacher::where('user_id', $this->uid())->find();
        $list = $this->model->where(['profile_id'=>$teacher['profile_id']])->with(['student', 'classes'])->limit($page, $limit)->select();
        $count = $this->model->where(['profile_id'=>$teacher['profile_id']])->count();
        $this->success('success', ['list'=> $list, 'count'=>$count]);
    }

    /**
     * 提问
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function make_question()
    {
        $uid = $this->request->post('uid');
        $question = $this->request->post('question');
        $classes_id = $this->request->post('classes_id', 0);

        if (!$uid) $this->error('请选择提问学生');
        if (!$question) $this->error('请输入提问问题');
        if (!$classes_id) $this->error('参数错误');

        $user = User::where('id', $uid)->find();
        if (!$user) $this->error('无效学生');
        $teacher = Teacher::where('user_id', $this->uid())->find();
        $res = $this->model->save([
            'profile_id' => $teacher['profile_id'],
            'question' => $question,
            'uid' => $uid,
            'classes_id' => $classes_id,
        ]);
        if ($res === false) $this->error('提问失败');

        $this->success('提问成功');
    }

    /**
     * 评价
     * @return void
     */
    public function criticize()
    {
        $id = $this->request->post('id');
        if (!$id) $this->error('参数错误');
        $question = $this->model->where('id', $id)->find();
        if (!$question) $this->error('问题不存在');
        $level = $this->request->post('level', 0);
        if (!$level) $this->error('星级评价不能为空');
        if ($question->status == 1) $this->error('学生还未回答');
        if ($question->status > 2) $this->error('已完成评价');
        $question->level = $level;
        $question->status = 3;
        $res = $question->save();
        if ($res === false) $this->error('评价失败');
        $this->success('评价成功');
    }

    /**
     * 发放勋章
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function send_badge()
    {
        $id = $this->request->post('id');
        if (!$id) $this->error('参数错误');
        $question = $this->model->where('id', $id)->find();
        if (!$question) $this->error('问题不存在');
        $is_send = $this->request->post('is_send', 1); //1发放 2不发放
        if ($question->status == 4) $this->error('勋章已发放，请勿重复发放');
        $question->status = 4;
        Db::startTrans();
        $res = $question->save();
        if ($res === false) {
            Db::rollback();
            $this->error('发放失败');
        }
        if ($is_send==1) {
            $userBadge = new UserBadge();
            $badgeRes = $userBadge->save([
                'user_id' => $question->uid,
                'type' => 2,
                'profile_id' => $question->profile_id,
                'level' => $question->level,
                'classes_id' => $question->classes_id
            ]);
            if ($badgeRes === false) {
                Db::rollback();
                $this->error('发放失败！');
            }
        }
        Db::commit();
        $this->success('成功');
    }

    /**
     * 生成勋章
     * @return void
     */
    public function make_badge()
    {
        $level = $this->request->post('level', 1);
        $userBadge = new UserBadge();
        $userBadge->type = 2;
        $userBadge->profile_id = $this->uid();
        $userBadge->level = $level;

        $badgeRes = $userBadge->save();
        if ($badgeRes === false) {
            $this->error('生成勋章失败！');
        }
        $this->success('生成成功', $userBadge->getLastInsID());
    }


}